<?php
// TODO: make it 'do not repeat your self'
// TODO: use subcontrollers?
// TODO: comment
// TODO: case when edit selection list is empty
// TODO: should all forms be separated?
// TODO: add special router for some actions?
// FIXME: make common information/error page template
class AdminController extends Zend_Controller_Action
{
	public function indexAction()
	{
		
	}
	
	protected function commonAdd($form, $modelClassName)
	{
		$request = $this->getRequest();
		if ($request->isPost() && $form->isValid($request->getPost())) {
			//var_dump($form->getValues());
			//var_dump($model);
			// TODO: check if proper class
			$model = new $modelClassName($form->getValues());
			$model->save();
			// TODO show info and/or redirect
			//$this->view->info = 'Succesfuly added';
			return true;
		} else {
			return false;
		}
	}
	
	protected function commonEdit($form, $model)
	{
		$request = $this->getRequest();
		
		if (!$request->isPost()) {
			//throw new Exception('Objects can be editet only by POST');
			// print form
			return $form;
		} else {
			$post 	= $request->getPost();
			$object = $model->get($post[$form->getIdFieldName()]);
			
			if (isset($post['doEdit'])) {
				$form->makeEdit($object);
			} elseif (isset($post['doDelete'])) {
				$object->delete();
			}
			
			return $form;
		}
	}
	
	protected function commonUpdate($form, $model)
	{
		$request = $this->getRequest();
		if ($request->isPost()) {
			$post = $request->getPost();
			$object = $model->get($post[$form->getIdFieldName()]);
			$object->setOptions($post);
			$object->save();
			return true;
		}
	}
	
// =================================================
// COUNTRY
// =================================================

	public function countrymainAction()
	{
		
	}
	
	public function countryaddAction()
	{			
		$form = new Plus_Form_Admin_Country_Add();
		$modelClassName = 'Plus_Model_Country_Country';
		if (!$this->commonAdd($form, $modelClassName)) {
			$this->view->form = $form;
		} else {
			$this->view->form = 'Country added';
		}
		
	}
	
	public function countryshowAction()
	{
		$model = new Plus_Model_Country_Country();
		$countries = $model->fetchAll();
		$form = new Plus_Form_Admin_Country_Edit();
		$form->makeList($countries);
		$this->view->form = $form;
	}
	
	public function countryeditAction()
	{
		$form = $this->commonEdit(new Plus_Form_Admin_Country_Edit(), 
			new Plus_Model_Country_Country());
		
		$this->view->form = $form;
	}
	
	
	
	public function countryupdateAction()
	{
		try {
			if ($this->commonUpdate(new Plus_Form_Admin_Country_Edit(), 
				new Plus_Model_Country_Country())) {
					$this->view->form = "Country updated.";
			}
		} catch (Exception $e) {
			$this->view->form = $e->getMessage();
		}
		
	}
	
// =================================================
// SERVICE VERSION
// =================================================
	public function serviceversionmainAction()
	{
		
	}
	
	public function serviceversionaddAction()
	{			
		$form = new Plus_Form_Admin_ServiceVersion_Add();
		$modelClassName = 'Plus_Model_ServiceVersion_ServiceVersion';
		if (!$this->commonAdd($form, $modelClassName)) {
			$this->view->form = $form;
		} else {
			$this->view->form = 'Service version added';
		}
		
	}
	
	public function serviceversionshowAction()
	{
		$model = new Plus_Model_ServiceVersion_ServiceVersion();
		$serviceVersions = $model->fetchAll();
		//var_dump($model->find(2));
		//$this->view->countries = array(1 => $model->find(2));
		//var_dump($countries);
		$form = new Plus_Form_Admin_ServiceVersion_Edit();
		$form->makeList($serviceVersions);
		$this->view->form = $form;
	}
	
	public function serviceversioneditAction()
	{
		$request = $this->getRequest();
		$form = new Plus_Form_Admin_ServiceVersion_Edit();
		if ($request->isPost()) {
			//var_dump($request->getPost());
			$post = $request->getPost();
			$id = $post['service_version'];
			$model = new Plus_Model_ServiceVersion_ServiceVersion();
			//var_dump($model);
			$model = $model->get($id);		
			if (isset($post['doEdit'])) {
				// edit country
				$form->makeEdit($model);
			} elseif (isset($post['doDelete'])) {
				// delete country 
				$model->delete();
				// TODO: inform/redirect
				$form = 'Service version deleted.';
			}
			
			//var_dump($model);
		}
		$this->view->form = $form;
	}
	
	public function serviceversionupdateAction()
	{
		$request = $this->getRequest();
		if ($request->isPost()) {
			//var_dump($request->getPost());
			$post = $request->getPost();
			$model = new Plus_Model_ServiceVersion_ServiceVersion();
			$model = $model->find($post['Id']);
			//var_dump($model);
			$model->setOptions($post);
			//var_dump($model);
			$model->save();
			// TODO: redirect instead following messages?
			$this->view->info = 'Service version Updated!';
		} else {
			$this->view->info = 'Service version NOT Updated!';
		}
	}
	
// =================================================
// MAIL CATEGORY
// =================================================

	public function mailcategorymainAction()
	{
		
	}
	
	public function mailcategoryaddAction()
	{			
		$form = new Plus_Form_Admin_MailCategory_Add();
		$modelClassName = 'Plus_Model_MailCategory_MailCategory';
		if (!$this->commonAdd($form, $modelClassName)) {
			$this->view->form = $form;
		} else {
			$this->view->form = 'Service version added';
		}
		
	}
	
	public function mailcategoryshowAction()
	{
		$model = new Plus_Model_MailCategory_MailCategory();
		$serviceVersions = $model->fetchAll();
		//var_dump($model->find(2));
		//$this->view->countries = array(1 => $model->find(2));
		//var_dump($countries);
		$form = new Plus_Form_Admin_MailCategory_Edit();
		$form->makeList($serviceVersions);
		$this->view->form = $form;
	}
	
	public function mailcategoryeditAction()
	{
		$request = $this->getRequest();
		$form = new Plus_Form_Admin_MailCategory_Edit();
		if ($request->isPost()) {
			//var_dump($request->getPost());
			$post = $request->getPost();
			$id = $post['mail_category'];
			$model = new Plus_Model_MailCategory_MailCategory();
			$model = $model->find($id);		
			//var_dump($model);
			if (isset($post['doEdit'])) {
				// edit country
				$form->makeEdit($model);
			} elseif (isset($post['doDelete'])) {
				// delete country 
				$model->delete();
				// TODO: inform/redirect
				$form = 'Service version deleted.';
			}
			
			//var_dump($model);
		}
		$this->view->form = $form;
	}
	
	public function mailcategoryupdateAction()
	{
		$request = $this->getRequest();
		if ($request->isPost()) {
			//var_dump($request->getPost());
			$post = $request->getPost();
			$model = new Plus_Model_MailCategory_MailCategory();
			$model = $model->find($post['Id']);
			//var_dump($model);
			$model->setOptions($post);
			//var_dump($model);
			$model->save();
			// TODO: redirect instead following messages?
			$this->view->info = 'Service version Updated!';
		} else {
			$this->view->info = 'Service version NOT Updated!';
		}
	}
	
}